//
//$$Id XMLRequestHandler.js $$
//
//Copyright 1997-2011, Fair Isaac Corporation. All Rights Reserved.
//
/**
 * @class This class provides methods to make an AJAX call.
 * @constructor
 * @param {String} url The URL to invoke for the AJAX request
 * @param {function} callback The call back function
 * @param {String} handlerName The name of the handler
 * @param {String} formInstanceId The form instance id (optional)
 */
function XMLRequestHandler(url, callback, handlerName, formInstanceId)
{
	/** @ignore */ 
	this.url = url;
	/** @ignore */ 
	this.callback = callback;
	/** @ignore */ 
	this.name = handlerName;
	/** @ignore */ 
	this.async = false;
	/** @ignore */ 
	this.formInstanceId = formInstanceId;
	/** @ignore */ 
	this.callbackArguments = arguments;

	// public methods
	
	this.get = _executeGetRequest;
	this.post = _executePostRequest;
	this.getNewHttpRequestObject = getNewHttpRequestObject;
	
	this.getName = getName;
	this.setName = setName;
	this.getURL = getURL;
	this.setURL = setURL;
	this.getFormInstanceId = getFormInstanceId;
	this.setFormInstanceId = setFormInstanceId;
	this.setAsync = setAsync;
	this.isAsync = isAsync;
	
	
	// methods	
	
	function setName(name)
	{
		this.name = name;
	}
	
	function getName()
	{
		return this.name;
	}
	
	function setURL(name)
	{
		this.url = url;
	}
	
	function getURL()
	{
		return this.url;
	}
	
	function setFormInstanceId(fid)
	{
		this.formInstanceId = fid;
	}
	
	function getFormInstanceId()
	{
		return this.formInstanceId;
	}
	
	/**
	* returns the XML document of this instance
	* @param {boolean} async A flag to make the request asynchronous.The request is synchronous by default
	*/
	function setAsync(async)
	{
		this.async = async;
	}
	
	/**
	* returns true if the request mode is asynchronous
	* @type boolean
	*/
	function isAsync()
	{
		return this.async;
	}
	
	
	// end methods

	/**
	* Executes a HTTP get request. The handling of response to the AJAX request is delegated
	* to the supplied callback function. The arguments supplied to the request in the constructor
	* are passed back to the callback method. The second argument is replaced with the XMLHttpRequest
	* object which can provide access to the response object
	*/
	function _executeGetRequest()
	{
	    var request = this.getNewHttpRequestObject();
	    var callback = this.callback;
	    var callbackArguments = this.callbackArguments;
		if(request) {
			var browserCallback = this.async || navigator.appName != "Netscape";
			if (browserCallback) {
				request.onreadystatechange = function () {
												if(request.readyState == 4) {
													callbackArguments[1] = request;
													callback.apply(this, callbackArguments);
												}
											  };
			}
			
			request.open("GET", this.url, this.async);
			request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
			request.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
			request.send(null);
			
			if (!browserCallback) {
				if(request.readyState == 4) {
					callbackArguments[1] = request;
					callback.apply(this, callbackArguments);
				}
			}
		}
	}
	
	/**
	* Executes a HTTP post request. The handling of response to the AJAX request is delegated
	* to the supplied callback function. The arguments supplied to the request in the constructor
	* are passed back to the callback method. The second argument is replaced with the XMLHttpRequest
	* object which can provide access to the response object
	*/
	function _executePostRequest(contentType, content)
	{
	    var request = this.getNewHttpRequestObject();
	    var callback = this.callback;
	    var callbackArguments = this.callbackArguments;
	
		if(request) {
			var browserCallback = this.async || navigator.appName != "Netscape";
			if (browserCallback) {
				request.onreadystatechange = function () {
											if(request.readyState == 4) {
												callbackArguments[1] = request;
												callback.apply(this, callbackArguments);
											}
										};
			}
			
			request.open("POST", url, this.async);
			request.setRequestHeader("Content-Type", contentType)
			request.send(content);
			if (!browserCallback) {
				if(request.readyState == 4) {
					callbackArguments[1] = request;
					callback.apply(this, callbackArguments);
				}
			}
		}
	}
	
	/**
	* Creates an XMLHttpRequest object specific to the browser. This method should be used only when 
	* you want to use the AJAX request with options not provided by the XMLRequestHandler
	*/
	function getNewHttpRequestObject()
	{
		var req = null;
		// branch for native XMLHttpRequest object
	    if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			var progIDs = [ 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0' ];
			for (var i = 0; i < progIDs.length; i++) {
				try {
					req = new ActiveXObject(progIDs[i]);
					if(undefined!=req && req!=null ) 
						break;
		        }
		        catch (ex) {
		        }
			}					
		}
		return req;
	}
}
